<?php

require_once 'smarty/Smarty-Start.class.php';

class Deia_Controller extends SmartyStart {

    /**
     * Instancia do modelo a ser manipulado pelo controlador
     * @var <boolean>
     */
    public $model = false;
    /**
     *  Prefixo do classe do controller e model
     * @var <string>
     */
    protected $name = false;
    /**
     * Nome completo da classe modelo
     * @var <string>
     */
    protected $modelClassName = false;
    /**
     * Nome completo da classe controladora
     * @var <string>
     */
    protected $controllerClassName = false;
    /**
     *
     * @var <type>
     */
    private $params = null;

    /**
     *
     * @var <type> 
     */
    protected $urlApp = null;

    /**
     * url das imagens
     * @var <type>
     */
    protected $urlAppImg = null;

    /**
     * Controlador da classe. É requirido o prefixo do nome da classe
     * @param <string> $name
     * @param <boolean> $model - Determina se o modelo será instanciado
     */
    public function __construct($name, $model = false) {
        $this->urlApp = "http://{$_SERVER['SERVER_NAME']}/";
        $this->urlAppImg = $this->urlApp . "public/img/";

        parent::__construct($this->urlApp);

        $this->name = $name;
        $this->controllerClassName = $this->name . "_Controller";
        $this->modelClassName = $this->name . "_Model";

        if ($model) {
            $this->model = new $this->modelClassName;
        }

        $this->params = explode("/", $_GET['params']);
    }

    /**
     * Função de callback. Isso é mágica pura!
     * @param <object> $class Objeto da classe que contém o método
     * @param <string> $function - Nome do método
     * @param <array> $params - Parametros
     * @return <undefined>
     */
    public function callBack() {
        $method = $this->params[1];
        $class = new $this->controllerClassName;

        if (method_exists($class, $method)) {
            return $class->$method($this->params);
        } else {
            $method = "index";
            return $class->$method($this->params);
        }
    }

    /**
     * Renderiza o retorno do controlador requisitado
     * @param <type> $name
     * @param <type> $params
     */
    public function renderPage() {
        echo $this->callBack();
    }

    /**
     *
     * @param <type> $string 
     */
    protected function splash($string) {
        echo "<script>window.alert('{$string}')</script>";
    }

    /**
     *
     * @param <type> $url 
     */
    protected function go($url) {
        echo "<script>window.location='{$url}'</script>";
    }

    /**
     *
     * @param <type> $string
     * @return <type> 
     */
    protected function retira_acentos($string) {
        $array1 = array(
            "á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì", "î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç", "nº",
            "Á", "À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó", "Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç", "Nº",
        );

        $array2 = array(
            "a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i", "i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c", "n.",
            "A", "A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O", "O", "O", "O", "O", "U", "U", "U", "U", "C", "N.",
        );

        return str_replace($array1, $array2, $string);
    }

    protected function render($content){
        $this->assign("path", $this->urlApp);
        $this->assign("path_img", $this->urlAppImg);
        $this->assign("content", $content);

        $this->display("page.tpl");
    }
}